package com.example.hanjyLearn;

import com.example.hanjyLearn.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/englishLearn")
public class EnglishController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 查询所有单词列表
     */
    @RequestMapping("/queryAllEnglishWords")
    public Result<List<Map<String, Object>>> queryAllEnglishWords(@RequestBody Map<String, String> param) {
        String sql = "SELECT id, english, chinese, learn_date AS date, answer, remark FROM english_word " +
                "order by learn_date";
        List<Map<String, Object>> englishWordList = this.jdbcTemplate.queryForList(sql);
        return Result.success(englishWordList);
    }
    /**
     * 设置该题的答题结果
     **/
    @RequestMapping("/setAnswer")
    public Result<Void> setAnswer(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        String answer = param.get("answer");
        //        操作数据库
        String sql = "UPDATE english_word SET answer = ?, answer_time = current_time WHERE id = ?";
        int rows = this.jdbcTemplate.update(sql, answer, id);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("操作失败");
        }
    }

    /**
     * 追加新学的英语单词
     * @param param
     * @return
     */
    @RequestMapping("/addNewEnglishWord")
    public Result<Void> addNewEnglishWord(@RequestBody Map<String, String> param) {
        String english = param.get("english");
        String chinese = param.get("chinese");
        String date = param.get("date");
        //        操作数据库
        String sql = "INSERT INTO english_word (english, chinese, learn_date) VALUES (?, ?, ?)";
        int rows = this.jdbcTemplate.update(sql, english, chinese, date);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("操作失败");
        }
    }

    /**
     * 设置错了，就要删除重新录入
     * @param param
     * @return
     */
    @RequestMapping("/deleteEnglishWord")
    public Result<Void> deleteEnglishWord(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        //        操作数据库
        String sql = "delete from english_word where id = ?";
        int rows = this.jdbcTemplate.update(sql, id);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("操作失败");
        }
    }
    /**
     * 获取指定范围的指定个数的单词，用来测验
     */
    @RequestMapping("/querySomeEnglishWords")
    public Result<List<Map<String, Object>>> querySomeEnglishWords(@RequestBody Map<String, String> param) {
        String num = param.get("num");
        String range = param.get("range");
        String sql = "SELECT id, english, chinese, learn_date AS date, answer, remark FROM english_word WHERE 1=1 ";
        if (range != null && range.equals("01")) {
            sql += "ORDER BY ID ";
        } else if (range != null && range.equals("02")) {
            sql += "ORDER BY ID DESC ";
        } else if (range != null && range.equals("03")) {
            sql += "ORDER BY RAND() ";
        }
        sql += "LIMIT " + num;
        List<Map<String, Object>> englishWordList = this.jdbcTemplate.queryForList(sql);
        return Result.success(englishWordList);
    }

    /**
     * 统计历史测验成总绩
     * @param param
     * @return
     */
    @RequestMapping("/getEnglishStatistics")
    public Result<Map<String, Integer>> getEnglishStatistics(@RequestBody Map<String, String> param) {
        Integer rightNum = this.jdbcTemplate.queryForObject("SELECT count(1) FROM english_word WHERE answer = 'right'", Integer.class);
        Integer rightWrongAllNum = this.jdbcTemplate.queryForObject("SELECT count(1) FROM english_word WHERE answer = 'right' OR answer = 'wrong'", Integer.class);
        Map<String, Integer> map = new HashMap<>();
        map.put("rightNum", rightNum);
        map.put("rightWrongAllNum", rightWrongAllNum);
        return Result.success(map);
    }
}
